home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / inflow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-03  |  14.1 KB  |  559 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "command.h"
  24. #include "signals.h"
  25. #include "terminal.h"
  26. #include "target.h"
  27.  
  28. #ifdef USG
  29. #include <sys/types.h>
  30. #endif
  31.  
  32. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  33.    is not defined) want this header, and it won't do any harm.  */
  34. #include <fcntl.h>
  35.  
  36. #include <sys/param.h>
  37. #include <signal.h>
  38.  
  39. static void
  40. kill_command PARAMS ((char *, int));
  41.  
  42. static void
  43. terminal_ours_1 PARAMS ((int));
  44.  
  45. extern struct target_ops child_ops;
  46.  
  47. /* Nonzero if we are debugging an attached outside process
  48.    rather than an inferior.  */
  49.  
  50. int attach_flag;
  51.  
  52.  
  53. /* Record terminal status separately for debugger and inferior.  */
  54.  
  55. /* Does GDB have a terminal (on stdin)?  */
  56. int gdb_has_a_terminal;
  57.  
  58. static TERMINAL sg_inferior;
  59. static TERMINAL sg_ours;
  60.  
  61. static int tflags_inferior;
  62. static int tflags_ours;
  63.  
  64. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  65. static struct tchars tc_inferior;
  66. static struct tchars tc_ours;
  67. #endif
  68.  
  69. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  70. static struct ltchars ltc_inferior;
  71. static struct ltchars ltc_ours;
  72. #endif
  73.  
  74. #ifdef TIOCLGET
  75. static int lmode_inferior;
  76. static int lmode_ours;
  77. #endif
  78.  
  79. #ifdef TIOCGPGRP
  80. # ifdef SHORT_PGRP
  81. static short pgrp_inferior;
  82. static short pgrp_ours;
  83. # else /* not def SHORT_PGRP */
  84. static int pgrp_inferior;
  85. static int pgrp_ours;
  86. # endif /* not def SHORT_PGRP */
  87. #else /* not def TIOCGPGRP */
  88. static void (*sigint_ours) ();
  89. static void (*sigquit_ours) ();
  90. #endif /* TIOCGPGRP */
  91.  
  92. /* The name of the tty (from the `tty' command) that we gave to the inferior
  93.    when it was last started.  */
  94.  
  95. static char *inferior_thisrun_terminal;
  96.  
  97. /* Nonzero if our terminal settings are in effect.
  98.    Zero if the inferior's settings are in effect.  */
  99.  
  100. static int terminal_is_ours;
  101.  
  102. /* Macro for printing errors from ioctl operations */
  103.  
  104. #define    OOPSY(what)    \
  105.   if (result == -1)    \
  106.     fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \
  107.         what, strerror (errno))
  108.  
  109. static void terminal_ours_1 ();
  110.  
  111. /* Initialize the terminal settings we record for the inferior,
  112.    before we actually run the inferior.  */
  113.  
  114. void
  115. terminal_init_inferior ()
  116. {
  117.   sg_inferior = sg_ours;
  118.   tflags_inferior = tflags_ours;
  119.  
  120. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  121.   tc_inferior = tc_ours;
  122. #endif
  123.  
  124. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  125.   ltc_inferior = ltc_ours;
  126. #endif
  127.  
  128. #ifdef TIOCLGET
  129.   lmode_inferior = lmode_ours;
  130. #endif
  131.  
  132. #ifdef TIOCGPGRP
  133.   pgrp_inferior = inferior_pid;
  134. #endif /* TIOCGPGRP */
  135.  
  136.   terminal_is_ours = 1;
  137. }
  138.  
  139. /* Put the inferior's terminal settings into effect.
  140.    This is preparation for starting or resuming the inferior.  */
  141.  
  142. void
  143. terminal_inferior ()
  144. {
  145.   int result;
  146.  
  147.   if (gdb_has_a_terminal && terminal_is_ours && inferior_thisrun_terminal == 0)
  148.     {
  149.       result = fcntl (0, F_SETFL, tflags_inferior);
  150.       result = fcntl (0, F_SETFL, tflags_inferior);
  151.       OOPSY ("fcntl F_SETFL");
  152.       result = ioctl (0, TIOCSETN, &sg_inferior);
  153.       OOPSY ("ioctl TIOCSETN");
  154.  
  155. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  156.       result = ioctl (0, TIOCSETC, &tc_inferior);
  157.       OOPSY ("ioctl TIOCSETC");
  158. #endif
  159. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  160.       result = ioctl (0, TIOCSLTC, <c_inferior);
  161.       OOPSY ("ioctl TIOCSLTC");
  162. #endif
  163. #ifdef TIOCLGET
  164.       result = ioctl (0, TIOCLSET, &lmode_inferior);
  165.       OOPSY ("ioctl TIOCLSET");
  166. #endif
  167.  
  168. #ifdef TIOCGPGRP
  169.       result = ioctl (0, TIOCSPGRP, &pgrp_inferior);
  170.       /* If we attached to the process, we might or might not be sharing
  171.      a terminal.  Avoid printing error msg if we are unable to set our
  172.      terminal's process group to his process group ID.  */
  173.       if (!attach_flag) {
  174.     OOPSY ("ioctl TIOCSPGRP");
  175.       }
  176. #else
  177.       sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
  178.       sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
  179. #endif /* TIOCGPGRP */
  180.     }
  181.   terminal_is_ours = 0;
  182. }
  183.  
  184. /* Put some of our terminal settings into effect,
  185.    enough to get proper results from our output,
  186.    but do not change into or out of RAW mode
  187.    so that no input is discarded.
  188.  
  189.    After doing this, either terminal_ours or terminal_inferior
  190.    should be called to get back to a normal state of affairs.  */
  191.  
  192. void
  193. terminal_ours_for_output ()
  194. {
  195.   terminal_ours_1 (1);
  196. }
  197.  
  198. /* Put our terminal settings into effect.
  199.    First record the inferior's terminal settings
  200.    so they can be restored properly later.  */
  201.  
  202. void
  203. terminal_ours ()
  204. {
  205.   terminal_ours_1 (0);
  206. }
  207.  
  208. static void
  209. terminal_ours_1 (output_only)
  210.      int output_only;
  211. {
  212.   int result;
  213. #ifdef TIOCGPGRP
  214.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  215.   void (*osigttou) ();
  216. #endif /* TIOCGPGRP */
  217.  
  218.   /* Checking inferior_thisrun_terminal is necessary so that
  219.      if GDB is running in the background, it won't block trying
  220.      to do the ioctl()'s below.  Checking gdb_has_a_terminal
  221.      avoids attempting all the ioctl's when running in batch.  */
  222.   if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal == 0)
  223.     return;
  224.  
  225.   if (!terminal_is_ours)
  226.     {
  227.       terminal_is_ours = 1;
  228.  
  229. #ifdef TIOCGPGRP
  230.       osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
  231.  
  232.       result = ioctl (0, TIOCGPGRP, &pgrp_inferior);
  233.       result = ioctl (0, TIOCSPGRP, &pgrp_ours);
  234.  
  235.       signal (SIGTTOU, osigttou);
  236. #else
  237.       signal (SIGINT, sigint_ours);
  238.       signal (SIGQUIT, sigquit_ours);
  239. #endif /* TIOCGPGRP */
  240.  
  241.       tflags_inferior = fcntl (0, F_GETFL, 0);
  242.       result = ioctl (0, TIOCGETP, &sg_inferior);
  243.  
  244. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  245.       result = ioctl (0, TIOCGETC, &tc_inferior);
  246. #endif
  247. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  248.       result = ioctl (0, TIOCGLTC, <c_inferior);
  249. #endif
  250. #ifdef TIOCLGET
  251.       result = ioctl (0, TIOCLGET, &lmode_inferior);
  252. #endif
  253.     }
  254.  
  255. #ifdef HAVE_TERMIO
  256.   sg_ours.c_lflag |= ICANON;
  257.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  258.     sg_ours.c_lflag &= ~ICANON;
  259. #else /* not HAVE_TERMIO */
  260.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  261.   if (output_only)
  262.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  263. #endif /* not HAVE_TERMIO */
  264.  
  265.   result = fcntl (0, F_SETFL, tflags_ours);
  266.   result = fcntl (0, F_SETFL, tflags_ours);
  267.   result = ioctl (0, TIOCSETN, &sg_ours);
  268.  
  269. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  270.   result = ioctl (0, TIOCSETC, &tc_ours);
  271. #endif
  272. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  273.   result = ioctl (0, TIOCSLTC, <c_ours);
  274. #endif
  275. #ifdef TIOCLGET
  276.   result = ioctl (0, TIOCLSET, &lmode_ours);
  277. #endif
  278.  
  279. #ifdef HAVE_TERMIO
  280.   sg_ours.c_lflag |= ICANON;
  281. #else /* not HAVE_TERMIO */
  282.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  283. #endif /* not HAVE_TERMIO */
  284.  
  285.   result = result;    /* lint */
  286. }
  287.  
  288. /* ARGSUSED */
  289. void
  290. term_info (arg, from_tty)
  291.      char *arg;
  292.      int from_tty;
  293. {
  294.   target_terminal_info (arg, from_tty);
  295. }
  296.  
  297. /* ARGSUSED */
  298. void
  299. child_terminal_info (args, from_tty)
  300.      char *args;
  301.      int from_tty;
  302. {
  303.   register int i;
  304.  
  305.   if (!gdb_has_a_terminal) {
  306.     printf_filtered ("This GDB does not control a terminal.\n");
  307.     return;
  308.   }
  309.  
  310. #ifdef TIOCGPGRP
  311.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  312.  
  313.   printf_filtered ("owner pgrp = %d, fcntl flags = 0x%x.\n",
  314.       pgrp_inferior, tflags_inferior);
  315. #endif /* TIOCGPGRP */
  316.  
  317. #ifdef HAVE_TERMIO
  318.  
  319.   printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
  320.       sg_inferior.c_iflag, sg_inferior.c_oflag);
  321.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  322.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  323.   printf_filtered ("c_cc: ");
  324.   for (i = 0; (i < NCC); i += 1)
  325.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  326.   printf_filtered ("\n");
  327.  
  328. #else /* not HAVE_TERMIO */
  329.  
  330.   printf_filtered ("sgttyb.sg_flags = 0x%x.\n", sg_inferior.sg_flags);
  331.  
  332. #endif /* not HAVE_TERMIO */
  333.  
  334. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  335.   printf_filtered ("tchars: ");
  336.   for (i = 0; i < (int)sizeof (struct tchars); i++)
  337.     printf_filtered ("0x%x ", ((unsigned char *)&tc_inferior)[i]);
  338.   printf_filtered ("\n");
  339. #endif
  340.  
  341. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  342.   printf_filtered ("ltchars: ");
  343.   for (i = 0; i < (int)sizeof (struct ltchars); i++)
  344.     printf_filtered ("0x%x ", ((unsigned char *)<c_inferior)[i]);
  345.   printf_filtered ("\n");
  346. #endif
  347.   
  348. #ifdef TIOCLGET
  349.   printf_filtered ("lmode:  0x%x\n", lmode_inferior);
  350. #endif
  351. }
  352.  
  353. /* NEW_TTY_PREFORK is called before forking a new child process,
  354.    so we can record the state of ttys in the child to be formed.
  355.    TTYNAME is null if we are to share the terminal with gdb;
  356.    or points to a string containing the name of the desired tty.
  357.  
  358.    NEW_TTY is called in new child processes under Unix, which will
  359.    become debugger target processes.  This actually switches to
  360.    the terminal specified in the NEW_TTY_PREFORK call.  */
  361.  
  362. void
  363. new_tty_prefork (ttyname)
  364.      char *ttyname;
  365. {
  366.   /* Save the name for later, for determining whether we and the child
  367.      are sharing a tty.  */
  368.   inferior_thisrun_terminal = ttyname;
  369. }
  370.  
  371. void
  372. new_tty ()
  373. {
  374.   register int tty;
  375.  
  376.   if (inferior_thisrun_terminal == 0)
  377.     return;
  378.  
  379. #ifdef TIOCNOTTY
  380.   /* Disconnect the child process from our controlling terminal.  */
  381.   tty = open("/dev/tty", O_RDWR);
  382.   if (tty > 0)
  383.     {
  384.       ioctl(tty, TIOCNOTTY, 0);
  385.       close(tty);
  386.     }
  387. #endif
  388.  
  389.   /* Now open the specified new terminal.  */
  390.  
  391. #ifdef USE_O_NOCTTY
  392.   tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
  393. #else
  394.   tty = open(inferior_thisrun_terminal, O_RDWR);
  395. #endif
  396.   if (tty == -1)
  397.     {
  398.       print_sys_errmsg (inferior_thisrun_terminal, errno);
  399.       _exit(1);
  400.     }
  401.  
  402.   /* Avoid use of dup2; doesn't exist on all systems.  */
  403.   if (tty != 0)
  404.     { close (0); dup (tty); }
  405.   if (tty != 1)
  406.     { close (1); dup (tty); }
  407.   if (tty != 2)
  408.     { close (2); dup (tty); }
  409.   if (tty > 2)
  410.     close(tty);
  411. }
  412.  
  413. /* Kill the inferior process.  Make us have no inferior.  */
  414.  
  415. /* ARGSUSED */
  416. static void
  417. kill_command (arg, from_tty)
  418.      char *arg;
  419.      int from_tty;
  420. {
  421.   if (inferior_pid == 0)
  422.     error ("The program is not being run.");
  423.   if (!query ("Kill the inferior process? "))
  424.     error ("Not confirmed.");
  425.   target_kill ();
  426.  
  427.   /* Killing off the inferior can leave us with a core file.  If so,
  428.      print the state we are left in.  */
  429.   if (target_has_stack) {
  430.     printf_filtered ("In %s,\n", current_target->to_longname);
  431.     if (selected_frame == NULL)
  432.       fputs_filtered ("No selected stack frame.\n", stdout);
  433.     else
  434.       print_stack_frame (selected_frame, selected_frame_level, 1);
  435.   }
  436. }
  437.  
  438. /* The inferior process has died.  Long live the inferior!  */
  439.  
  440. void
  441. generic_mourn_inferior ()
  442. {
  443.   inferior_pid = 0;
  444.   attach_flag = 0;
  445.   mark_breakpoints_out ();
  446.   registers_changed ();
  447.  
  448. #ifdef CLEAR_DEFERRED_STORES
  449.   /* Delete any pending stores to the inferior... */
  450.   CLEAR_DEFERRED_STORES;
  451. #endif
  452.  
  453.   reopen_exec_file ();
  454.   if (target_has_stack) {
  455.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  456.                       read_pc ()));
  457.     select_frame (get_current_frame (), 0);
  458.   } else {
  459.     set_current_frame (0);
  460.     select_frame ((FRAME) 0, -1);
  461.   }
  462.   /* It is confusing to the user for ignore counts to stick around
  463.      from previous runs of the inferior.  So clear them.  */
  464.   breakpoint_clear_ignore_counts ();
  465. }
  466.  
  467. void
  468. child_mourn_inferior ()
  469. {
  470.   unpush_target (&child_ops);
  471.   generic_mourn_inferior ();
  472. }
  473.  
  474. #if 0 
  475. /* This function is just for testing, and on some systems (Sony NewsOS
  476.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  477.    (since on this system at least sys/time.h is not protected against
  478.    multiple inclusion).  */
  479. /* ARGSUSED */
  480. static void
  481. try_writing_regs_command (arg, from_tty)
  482.      char *arg;
  483.      int from_tty;
  484. {
  485.   register int i;
  486.   register int value;
  487.  
  488.   if (inferior_pid == 0)
  489.     error ("There is no inferior process now.");
  490.  
  491.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  492.      kernel panic if we try to write past the end of the user area.
  493.      Presumably Sun will fix this bug (it has been reported), but it
  494.      is tacky to crash the system, so at least on SunOS4 we need to
  495.      stop writing when we hit the end of the user area.  */
  496.   for (i = 0; i < sizeof (struct user); i += 2)
  497.     {
  498.       QUIT;
  499.       errno = 0;
  500.       value = call_ptrace (3, inferior_pid, i, 0);
  501.       call_ptrace (6, inferior_pid, i, value);
  502.       if (errno == 0)
  503.     {
  504.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  505.           i, value, value);
  506.     }
  507.       else if ((i & 0377) == 0)
  508.     printf (" Failed at 0x%x.\n", i);
  509.     }
  510. }
  511. #endif
  512.  
  513. void
  514. _initialize_inflow ()
  515. {
  516.   int result;
  517.  
  518.   add_info ("terminal", term_info,
  519.        "Print inferior's saved terminal status.");
  520.  
  521. #if 0
  522.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  523.        "Try writing all locations in inferior's system block.\n\
  524. Report which ones can be written.");
  525. #endif
  526.  
  527.   add_com ("kill", class_run, kill_command,
  528.        "Kill execution of program being debugged.");
  529.  
  530.   inferior_pid = 0;
  531.  
  532.   /* Get all the current tty settings (including whether we have a tty at
  533.      all!).  */
  534.  
  535.   tflags_ours = fcntl (0, F_GETFL, 0);
  536.  
  537.   result = ioctl (0, TIOCGETP, &sg_ours);
  538.   if (result == 0) {
  539.     gdb_has_a_terminal = 1;
  540.     /* Get the rest of the tty settings, then... */
  541. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  542.     ioctl (0, TIOCGETC, &tc_ours);
  543. #endif
  544. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  545.     ioctl (0, TIOCGLTC, <c_ours);
  546. #endif
  547. #ifdef TIOCLGET
  548.     ioctl (0, TIOCLGET, &lmode_ours);
  549. #endif
  550. #ifdef TIOCGPGRP
  551.     ioctl (0, TIOCGPGRP, &pgrp_ours);
  552. #endif /* TIOCGPGRP */
  553.   } else {
  554.     gdb_has_a_terminal = 0;
  555.   }
  556.  
  557.   terminal_is_ours = 1;
  558. }
  559.